package com.mediacloud.app.newsmodule.fragment.imagetext

import android.content.Context
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.core.view.isVisible
import com.mediacloud.app.newsmodule.R
import xyz.doikki.videoplayer.controller.ControlWrapper
import xyz.doikki.videoplayer.controller.IControlComponent
import xyz.doikki.videoplayer.player.VideoView

class IWindCloseComponent(context: Context) : FrameLayout(context), IControlComponent {
    val closeIv = ImageView(context)
    var show = false
        set(value) {
            field = value
            if (value) {
                visibility = if(isHideClose){
                    View.VISIBLE
                }else {
                    View.GONE
                }
                bringToFront()
            } else {
                visibility = View.GONE
            }
        }

    var isHideClose = false
    var isFull = false

    init {
        closeIv.setImageResource(R.drawable.player_win_close)
        val lp = LayoutParams(-2, -2)
        lp.gravity = Gravity.END
        addView(closeIv, lp)
        visibility = View.GONE
    }

    override fun attach(controlWrapper: ControlWrapper) {

    }

    override fun getView(): View? {
        return this
    }

    override fun onVisibilityChanged(isVisible: Boolean, anim: Animation?) {
        isHideClose = isVisible
        if (show && !isFull) {
            this.isVisible = isVisible
        }
    }

    override fun onPlayStateChanged(playState: Int) {
        when (playState) {
            VideoView.STATE_PLAYBACK_COMPLETED,
            VideoView.STATE_IDLE,
            VideoView.STATE_PREPARING,
            VideoView.STATE_ERROR,
            VideoView.STATE_START_ABORT,
            -> {
                visibility = View.GONE
            }
            else -> {
                if (show) {
                    bringToFront()
                    visibility = View.VISIBLE
                } else {
                    visibility = View.GONE
                }
            }

        }

    }

    override fun onPlayerStateChanged(playerState: Int) {
        if (playerState == VideoView.PLAYER_FULL_SCREEN) {
            visibility = View.GONE
            isFull = true
        } else {
            isFull = false
            if (show) {
                bringToFront()
                visibility = View.VISIBLE
            } else {
                visibility = View.GONE
            }
        }
    }

    override fun setProgress(duration: Int, position: Int) {

    }

    override fun onLockStateChanged(isLocked: Boolean) {

    }
}